<?php require('connections/con_gl.php'); ?>
<?php
cekAkses($_SESSION[akses],"PD-2");
$currentPage = $_SERVER["PHP_SELF"];
// delete stok 0 atau barang tidak ada
mysql_select_db($database_con_gl, $con_gl);
$query_delstok = "delete from gl_stok  where stok <= 0 OR barang='' OR barang='0'";
$delstok       = mysql_query($query_delstok) or die(mysql_error());
// ---
$maxRows_data = 8;
$pageNum_data = 0;
if (isset($_GET['pageNum_data'])) {
  $pageNum_data = $_GET['pageNum_data'];
}
$startRow_data = $pageNum_data * $maxRows_data;
$cr_txt = str_replace("+"," ",$_REQUEST[cr_txt]);
mysql_select_db($database_con_gl, $con_gl);
$query_data = "SELECT gl_stok.id, gl_stok.stok, gl_stok.fisik, gl_gudang.id as gdgID, gl_gudang.gudang, gl_barang.id as brgID, gl_barang.barang, gl_satuan.id as satID, gl_satuan.satuan FROM gl_stok, gl_gudang, gl_barang, gl_satuan WHERE gl_gudang.id=gl_stok.gudang AND gl_barang.id=gl_stok.barang AND gl_satuan.id=gl_stok.satuan";
if($_GET[cr_txt]<>'' AND $_GET[cr_txt]<>'+') { 
$decode_cr = str_replace("+"," ",$_GET[cr_txt]);
$query_data .=" AND (gl_gudang.gudang LIKE '%%$decode_cr%%' OR gl_barang.barang LIKE '%%$decode_cr%%' OR gl_satuan.satuan LIKE '%%$decode_cr%%')";
}
$query_data .=" ORDER BY gl_gudang.gudang, gl_barang.barang, gl_satuan.satuan";
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$totalRows_data = mysql_num_rows($data);

$totalPages_data = floor($totalRows_data / $maxRows_data);

$query_data .=" LIMIT $startRow_data, $maxRows_data";
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$totalRow_data = mysql_num_rows($data);
$row_data = mysql_fetch_assoc($data);

$queryString_data = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_data") == false && 
        stristr($param, "totalRows_data") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_data = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_data = sprintf("&totalRows_data=%d%s", $totalRows_data, $queryString_data);
include('include/widget_export_stok.php');
?><h1>Stok Barang</h1>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable"> 
  <tr align="right" class="hide">
    <td colspan="8"><form id="search" name="search" method="post" action="">
      <label>
      <input name="cr_txt" type="text" id="cr_txt" value="<?php echo $_REQUEST['cr_txt'];?>" size="30" maxlength="100" />
      </label>
      <label>
      <input name="Search" type="submit" id="Search" value="Search" />
      </label>
    </form>
	<?php
	if(isset($_POST['Search'])) {
		echo "<script>window.location=\"index.php?component=stokopname&cr_txt=".str_replace(" ","+",$_POST[cr_txt])."\";</script>";
	}
	?>	</td>
  </tr>
  <!--<tr>
    <td colspan="7">| <a href="content/persediaan/addpenyesuaian.php" onclick="NewWindow(this.href,'name','900','500','yes');return false">Tambah</a> | </td>
  </tr>-->
  <tr>
    <th width="20%"><strong>Lokasi Kantor</strong></th>
    <th><strong>Barang</strong></th>
    <th width="10%" align="center"><strong>Satuan</strong></th>
    <th width="10%" align="left"><strong>Stok (Buku)</strong></th>
    <th width="10%"><strong>Stok (Fisik)</strong></th>
    <th width="10%" align="left"><strong>Selisih</strong></th>
    <th width="12%" align="center"><strong>Harga Pokok </strong></th>
    <th width="8%" align="center" class="hide"><strong>Aksi</strong></th>
  </tr>
  <?php if($totalRows_data > 0) { ?>
  <?php do { ?>
  <?php
mysql_select_db($database_con_gl, $con_gl);
$query_hitung = "SELECT sum(gl_trans.total) FROM gl_trans WHERE gl_trans.no_ref='$row_data[no_ref]' AND pos='K'";
$hitung = mysql_query($query_hitung, $con_gl) or die(mysql_error());
$row_hitung = mysql_fetch_assoc($hitung);
$totalRows_hitung = mysql_num_rows($hitung);

mysql_select_db($database_con_gl, $con_gl);
$query_getbeli = "SELECT gl_trans.hargasatuan, gl_rtrans.no_ref FROM gl_trans, gl_rtrans WHERE gl_rtrans.no_ref = gl_trans.no_ref AND gl_trans.hargasatuan<>'' AND gl_rtrans.jenis =6 AND gl_rtrans.gudang='$row_data[gdgID]' AND gl_trans.satuan='$row_data[satID]'";
$getbeli = mysql_query($query_getbeli, $con_gl) or die(mysql_error());
$row_getbeli = mysql_fetch_assoc($getbeli);
$totalRows_getbeli = mysql_num_rows($getbeli);
$totharga = 0; 
if($totalRows_getbeli > 0) {
do {
$totharga += $row_getbeli['hargasatuan'];
} while ($row_getbeli = mysql_fetch_assoc($getbeli));
$hpp = $totharga / $totalRows_getbeli;
}
  ?>
    <tr valign="top" bgcolor="#FFFFFF">
      <td><?php echo $row_data['gudang']; ?></td>
      <td><?php echo $row_data['barang']; ?></td>
      <td align="center"><?php echo $row_data['satuan']; ?></td>
      <td align="right"><?php echo intval($row_data['stok']); ?></td>
      <td align="right"><?php echo intval($row_data['fisik']); ?></td>
      <td align="right" bgcolor="#FFFFFF"><?php echo intval($row_data['stok'] - $row_data['fisik']);?></td>
      <td align="right" bgcolor="#FFFFFF"><?php echo number_format($hpp,0,',','.').",-";?></td>
      <td align="center" bgcolor="#FFFFFF" class="hide"><?php if($row_data['stok'] <> $row_data['fisik']) { ?><a href="index.php?component=stokopname&task=editstok&id=<?php echo $row_data[id];?>"><img src="images/edit_.png" border="0"/></a><?php }else{ ?><i>Sesuai</i><?php } ?></td>
    </tr>
    <?php } while ($row_data = mysql_fetch_assoc($data)); ?>
	<?php if($totalRows_data > $maxRows_data) { ?>
    <tr class="hide">
      <td colspan="10"><table border="0" align="left" width="50%">
          <tr>
            <td width="3%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, 0, $queryString_data); ?>"><img src="First.gif" border=0 ></a>
                <?php } // Show if not first page ?>            </td>
            <td width="3%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, max(0, $pageNum_data - 1), $queryString_data); ?>"><img src="Previous.gif" border=0></a>
                <?php } // Show if not first page ?>            </td>
            <td width="3%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, min($totalPages_data, $pageNum_data + 1), $queryString_data); ?>"><img src="Next.gif" border=0></a>
                <?php } // Show if not last page ?>            </td>
            <td width="3%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, $totalPages_data, $queryString_data); ?>"><img src="Last.gif" border=0></a>
                <?php } // Show if not last page ?>            </td>
			<td width="88%">Halaman : <?php echo $pageNum_data+1; ?>, Ditampilkan <?php echo $totalRow_data; ?> dari <?php echo $totalRows_data; ?> total data</td>
          </tr>
        </table></td>
    </tr>
	<?php } ?>
   <?php }else{ ?>
  <tr bgcolor="#FFFFFF" class="hide">
    <td colspan="8">Data tidak ada !!! </td>
  </tr>
  <?php } ?>
  <!--<tr>
    <td colspan="7">
	| <a href="content/persediaan/addpenyesuaian.php" onClick="NewWindow(this.href,'name','900','500','yes');return false">Tambah</a> | </td>
  </tr>-->
</table>
